package com.inool.controller;

import com.inool.common.result.Result;
import com.inool.model.Attendance;
import com.inool.service.AttendanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "考勤管理")
@RestController
@RequestMapping("/api/attendances")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    @Operation(summary = "添加考勤记录")
    @PostMapping
    public Result<Attendance> addAttendance(@RequestBody Attendance attendance) {
        Attendance newAttendance = attendanceService.addAttendance(attendance);
        return Result.ok(newAttendance);
    }

    @Operation(summary = "修改考勤记录信息")
    @PostMapping("/update/{id}")
    public Result<Attendance> updateAttendance(@PathVariable Long id, @RequestBody Attendance attendance) {
        Attendance updatedAttendance = attendanceService.updateAttendance(id, attendance);
        if (updatedAttendance != null) {
            return Result.ok(updatedAttendance);
        }
        return Result.fail(213,"未找到该考勤记录");
    }

    @Operation(summary = "删除考勤记录")
    @PostMapping("/delete/{id}")
    public Result<String> deleteAttendance(@PathVariable Long id) {
        attendanceService.deleteAttendance(id);
        return Result.ok("删除成功");
    }

    @Operation(summary = "获取考勤记录信息")
    @GetMapping("/{id}")
    public Result<Attendance> getAttendanceById(@PathVariable Long id) {
        Attendance attendance = attendanceService.getAttendanceById(id);
        if (attendance != null) {
            return Result.ok(attendance);
        }
        return Result.fail(213,"未找到该考勤记录");
    }

    @Operation(summary = "获取所有考勤记录信息")
    @GetMapping
    public Result<List<Attendance>> getAllAttendances() {
        List<Attendance> attendances = attendanceService.getAllAttendances();
        return Result.ok(attendances);
    }

    @Operation(summary = "根据学生 ID 获取考勤记录信息")
    @GetMapping("/student/{studentId}")
    public Result<List<Attendance>> getAttendancesByStudentId(@PathVariable String studentId) {
        List<Attendance> attendances = attendanceService.getAttendancesByStudentId(studentId);
        return Result.ok(attendances);
    }

    @Operation(summary = "根据课程 ID 获取考勤记录信息")
    @GetMapping("/course/{courseId}")
    public Result<List<Attendance>> getAttendancesByCourseId(@PathVariable String courseId) {
        List<Attendance> attendances = attendanceService.getAttendancesByCourseId(courseId);
        return Result.ok(attendances);
    }
}    